﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace SSOServer.Config
{
    public class SSOConfig
    {
        private static readonly object _lockKey = new object();
        private static SSOConfig _instance = null;
        private SSOSettingsSection dataContainer = null;

        protected SSOConfig()
        {
            dataContainer = ConfigurationManager.GetSection("ssoSettings") as SSOSettingsSection;
        }

        public static SSOConfig Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_lockKey)
                    {
                        if (_instance == null)
                        {
                            _instance = new SSOConfig();
                        }
                    }
                }
                return _instance;
            }
        }

        public SiteElement GetSite(string siteId)
        {
            SiteElement site = dataContainer.Sites[siteId];
            if (site == null)
            {
                throw new ConfigurationErrorsException(String.Format("Can't find site config named '{0}'.", siteId));
            }
            return site;
        }

        public GlobalElement GlobalSettings
        {
            get
            {
                if (dataContainer.GlobalSettings == null)
                {
                    throw new ConfigurationErrorsException("Can't find config element named global.");
                }
                return dataContainer.GlobalSettings;
            }
        }

    }
}
